package vista.paneles;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.ListSelectionModel;

@SuppressWarnings("serial")
public class ListConModelo extends JList {
	
	private DefaultListModel modeloLista;
	
	public ListConModelo()
	{
		super(new DefaultListModel());

		modeloLista = (DefaultListModel) this.getModel();
		
		this.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
	}
	
	public void agregarObjeto(Object objeto)
	{
		if (objeto == null)
			return;
		
		this.modeloLista.addElement(objeto);
	}
	
	@SuppressWarnings("unchecked")
	public void agregarObjetos(List<?> objetos)
	{
		Iterator<Object> iterador = (Iterator<Object>) objetos.iterator();
		
		while (iterador.hasNext())
			this.agregarObjeto(iterador.next());
	}
	
	public void quitarObejto()
	{
		if (this.getSelectedIndex()!=-1) 
			this.modeloLista.removeElementAt(this.getSelectedIndex());
	}
	
	public Object getObjetoSeleccionado()
	{
		if (this.getSelectedIndex()!=-1) 
			return this.modeloLista.get(this.getSelectedIndex());
		
		return null;
	}
	
	public List<?> getObjetos()
	{
		List<Object> objetos = new LinkedList<Object>();
		
		int nroElemento = 0;
		while (nroElemento < this.modeloLista.getSize())
		{
			objetos.add(this.modeloLista.get(nroElemento));
			nroElemento++;
		}
		
		return objetos;
	}

}
